<?php

	require_once '../Config.php';
	class SqlHelper {
		public $conn;
		public $host = host;
		public $dbname = db;
		public $username = username;
		public $password = password;
		
		public function __construct() {
			$this->conn = mysql_connect($this->host, $this->username, $this->password);
			if (!$this->conn) {
				die("连接失败".mysql_error());
			}
			mysql_select_db($this->dbname, $this->conn);
			mysql_query("set names 'utf8'");
		}
		
		public function query_row($sql) {
			$arr = array();
			$res = mysql_query($sql, $this->conn) or die(mysql_error());
			while ($row = mysql_fetch_row($res)) {
				$arr[] = $row;
			}
			mysql_free_result($res);
			return $arr;
		}
		
		public function query_assoc($sql) {
			$arr = array();
			$res = mysql_query($sql, $this->conn) or die(mysql_error());
			while ($row = mysql_fetch_assoc($res)) {
				$arr[] = $row;
			}
			mysql_free_result($res);
			return $arr;
		}
		
		public function query_page($sql, $page) {
			$res = mysql_query($sql, $this->conn) or die(mysql_error());
			$arr = array();
			while ($row = mysql_fetch_assoc($res)) {
				$arr[] = $row;
			}
			mysql_free_result($res);
			$page->data = $arr;
			$startIndex = strpos($sql, "select") + strlen("select");
			$search = substr($sql, $startIndex, strpos($sql, "from") - $startIndex);
			$search2 = substr($sql, strpos($sql, "limit"));
			$sql2 = str_replace($search2, "", str_replace($search, " count(id) ", $sql));
			$res2 = mysql_query($sql2, $this->conn) or die(mysql_error());
			if ($row = mysql_fetch_row($res2)) {
				$page->rowCount = $row[0];
				$page->pageCount = ceil($row[0]/$page->pageSize);
			}
			mysql_free_result($res2);
			$queryString = "";
			if ($page->params && count($page->params) > 0) {
				foreach ($page->params as $key=>$val) {
					$queryString.="&{$key}={$val}";
				}
			}
			$navigator = "";
			$navigator.="共&nbsp;{$page->rowCount}&nbsp;条信息，当前第&nbsp;{$page->pageNow}&nbsp;页，共&nbsp;{$page->pageCount}&nbsp;页";
			if ($page->pageNow > 1) {
				$prePage = $page->pageNow - 1;
				$navigator.="&nbsp;<a href='{$page->gotoUrl}?pageNow=1{$queryString}'>首页</a>&nbsp;<a href='{$page->gotoUrl}?pageNow={$prePage}{$queryString}'>上一页</a>";
			} else {
				$navigator.="&nbsp;<a>首页</a>&nbsp;<a>上一页</a>";
			}
			if ($page->pageNow < $page->pageCount) {
				$nextPage = $page->pageNow + 1;
				$navigator.="&nbsp;<a href='{$page->gotoUrl}?pageNow={$nextPage}{$queryString}'>下一页</a>&nbsp;<a href='{$page->gotoUrl}?pageNow={$page->pageCount}{$queryString}'>末页</a>";
			} else {
				$navigator.="&nbsp;<a>下一页</a>&nbsp;<a>末页</a>";
			}
				
			$page->navigator = $navigator;
		}
		
		public function update($sql) {
			$result = mysql_query($sql, $this->conn) or die(mysql_error());
			if (!$result) {
				return 0;
			} else {
				if (mysql_affected_rows($this->conn) > 0) {
					return 1;//表示执行ok
				} else {
					return 2;//表示没有行受到影响
				}
			}
		}
		
		public function close_connect() {
			if (!empty($this->conn)) {
				mysql_close($this->conn);
			}
		}
		
	}

?>